package com.chen.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.domain.UserWallet;
import com.chen.dto.R;
import com.chen.service.UserWalletService;
import com.chen.utils.UserUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: chen
 * @Date: 2021/4/26 11:07 AM
 * @Version 1.0
 */
@RestController
@Api(tags = "用户钱包接口控制器")
@RequestMapping("/userWallets")
public class UserWalletsController {

    @Autowired
    private UserWalletService userWalletService;

    @GetMapping("/getCoinAddress/{coinId}")
    public R<List<UserWallet>> userList(@PathVariable("coinId") Long coinId){
        List<UserWallet> list = userWalletService
                .list(new LambdaQueryWrapper<UserWallet>()
                        .eq(UserWallet::getCoinId, coinId)
                        .eq(UserWallet::getUserId, UserUtil.getUser())
                        .orderByDesc(UserWallet::getCreated));
        return R.ok(list);
    }

    @PostMapping
    public R save(@RequestBody UserWallet userWallet){
        userWalletService.saveUserWallet(userWallet);
        return R.ok();

    }

    @GetMapping
    @ApiOperation("根据id查询用户的钱包地址")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户的Id"),
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "size", value = "每页显示的条数")
    })
    @PreAuthorize("hasAuthority('user_query')")
    public R<Page<UserWallet>> list(Page<UserWallet> page, String userId) {

        QueryWrapper<UserWallet> userWalletQueryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(userId)) {
            userWalletQueryWrapper.eq("user_id", userId);

        }
        userWalletQueryWrapper.orderByDesc("last_update_time");
        Page<UserWallet> page1 = userWalletService.page(page, userWalletQueryWrapper);
        return R.ok(page1);
    }

}
